using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using Unity.VisualScripting;

namespace Hotran.Audio
{
    [RequireComponent(typeof(AudioSource))]
    public class AudioPlayer : MonoBehaviour
    {
        public static AudioPlayer Instance { get; private set; }

        [SerializeField] AudioSource bgm;

        private AudioSource audioSource;

        private float bgmMaxVolume = 1f;
        private readonly float bgmMinVolume = 0.3f;
        private Tweener bgmTween;

        private bool canPlaySound = true;

        private void Awake()
        {
            Instance = this;
            audioSource = GetComponent<AudioSource>();

            bgmMaxVolume = bgm.volume;
        }

        public void Disable() => canPlaySound = false;

        public void PlaySound(AudioClip audioClip, float volume = 1f)
        {
            if (canPlaySound)
                audioSource.PlayOneShot(audioClip, volume);
        }

        public void PlayRandom(params AudioClip[] audioClips)
        {
            int index = Random.Range(0, audioClips.Length);
            PlaySound(audioClips[index]);
        }

        public void BgmFadeOut()
        {
            bgmTween?.Kill();
            bgmTween = bgm.DOFade(bgmMinVolume, 0.5f);
        }
    }
}

